const Generator = require('yeoman-generator');
const fs = require('fs');
const path = require('path');

module.exports = class extends Generator {
    prompting() {
        return this.prompt({
            type: 'input',
            name: 'projectName',
            message: 'Project Name is: ',
            default: this.appname
        }).then(answers => this.answers = answers);
    }

    writing() {
        function copyTemplateFiles(dir) {
            fs.readdir(dir, (error, files) => {
                if (error) {
                    console.log(`Failed to read template files because ${error}`);
                    return;
                }
    
                files.forEach(file => fs.stat(path.join(dir, file), (error, stats) => {
                    if (error) {
                        console.log(`Failed to read template file because ${error}`);
                        return;
                    }

                    let srcPath = path.join(dir, file);
                    let dstPath = srcPath.replace(this.templatePath('.'), '');

                    if (stats.isFile()) {
                        this.fs.copyTpl(srcPath, '.' + this.destinationPath(dstPath), this.answers)
                        return;
                    }
    
                    copyTemplateFiles.call(this, path.join(dir, file));
                }));
            });
        }

        copyTemplateFiles.call(this, this.templatePath('.'));
    }
}